/*
 * jngc, Java utility classes by Nicholas Coltharp
 * Copyright (C) 2010 Nicholas Coltharp <ngc1@rice.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.rice.ngc1.util;

/**
 * <p>
 * Utility class to aid in the implementation of
 * {@link java.lang.Object#hashCode()}. By using this class's methods,
 * hash code calculation can be abstracted and reused across multiple
 * classes.
 * </p>
 * 
 * <p>
 * To implement {@code hashCode} with this class's methods, first call
 * {@link #getInitialValue()} to get an initial value for the hash
 * code. Then, for each field that affects the class's implementation
 * of {@link java.lang.Object#equals(Object)}, call
 * {@code hash(currentCode, field)}. If the class's {@code equals()}
 * method chains to the superclass's {@code equals()} method, then you
 * should submit the superclass's hash code at some point. Finally,
 * once this process has been repeated for every relevant field, you
 * can return the final result.
 * </p>
 * 
 * <p>
 * Note that if your {@code equals()} method involves checking fields
 * for reference equality, you should not submit those fields
 * directly; instead, submit the result of
 * {@link java.lang.System#identityHashCode(Object)}. Also note that
 * if your {@code equals()} method involves checking the values of
 * arrays, you can get a suitable array hash code from
 * {@link java.util.Arrays#deepHashCode(Object[])}.
 * </p>
 * 
 * This implementation comes from <a
 * href="http://www.technofundo.com/tech/java/equalhash.html">
 * technofundo.com</a>.
 */
public final class HashCodeGenerator {

    /**
     * Private constructor to prevent construction.
     */
    private HashCodeGenerator() {}

    /**
     * Returns a nonzero integer for use as the initial hash value.
     * 
     * @return the initial hash value
     */
    public static int getInitialValue() {
        return 1;
    }

    /**
     * Performs a single step of the hash code calculation with an
     * {@code int} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, int val) {
        return (31 * currentHash) + val;
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code boolean} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, boolean val) {
        return hash(currentHash, val ? 1 : 0);
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code char} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, char val) {
        return hash(currentHash, (int) val);
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code short} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, short val) {
        return hash(currentHash, (int) val);
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code long} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, long val) {
        return hash(currentHash, Primitives.longToIntBits(val));
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code float} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, float val) {
        return hash(currentHash, Float.floatToIntBits(val));
    }

    /**
     * Performs a single step of the hash code calculation with a
     * {@code double} value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, double val) {
        return hash(currentHash, Double.doubleToLongBits(val));
    }

    /**
     * Performs a single step of the hash code calculation with a
     * reference value.
     * 
     * @param currentHash
     *            the current value of the hash code
     * @param val
     *            the value to hash
     * @return the calculated hash code
     */
    public static int hash(int currentHash, Object val) {
        return hash(currentHash, val == null ? 0 : val.hashCode());
    }
}
